/*
 * QLinkButtonCustomizer.java
 *
 * Created on Jul 28, 2010, 9:50:59 PM
 */
package org.netbeans.modules.qcubedpalette.qlinkbutton;

import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;

/**
 * QLinkButtonCustomizer
 * The popup dialog that appears when the palette item is dragged into the
 * editor window.
 */
public class QLinkButtonCustomizer extends javax.swing.JPanel {

	private Dialog dialog = null;
	private DialogDescriptor descriptor = null;
	private boolean dialogOK = false;
	private QLinkButton qlnk;

	public QLinkButtonCustomizer(QLinkButton qlnk) {
		this.qlnk = qlnk;
		initComponents();
	}

	public boolean showDialog() {

		dialogOK = false;
		String displayName = "QLinkButton";

		descriptor = new DialogDescriptor(this,
				"Insert " + displayName, true,
				DialogDescriptor.OK_CANCEL_OPTION,
				DialogDescriptor.OK_OPTION,
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						if (descriptor.getValue().equals(DialogDescriptor.OK_OPTION)) {
							evaluateInput();
							dialogOK = true;
						}
						dialog.dispose();
					}
				});

		dialog = DialogDisplayer.getDefault().createDialog(descriptor);
		dialog.setVisible(true);
		dialog.setFocusTraversalKeysEnabled(true);
		repaint();

		return dialogOK;
	}

	private void evaluateInput() {
		String name = txtName.getText();
		String text = txtText.getText();
		this.qlnk.setName(name);
		this.qlnk.setText(text);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtText = new javax.swing.JTextField();

        jLabel1.setText(org.openide.util.NbBundle.getMessage(QLinkButtonCustomizer.class, "QLinkButtonCustomizer.jLabel1.text")); // NOI18N

        jLabel2.setText(org.openide.util.NbBundle.getMessage(QLinkButtonCustomizer.class, "QLinkButtonCustomizer.jLabel2.text")); // NOI18N

        txtName.setText(org.openide.util.NbBundle.getMessage(QLinkButtonCustomizer.class, "QLinkButtonCustomizer.txtName.text")); // NOI18N

        txtText.setText(org.openide.util.NbBundle.getMessage(QLinkButtonCustomizer.class, "QLinkButtonCustomizer.txtText.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(txtName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                    .add(txtText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(txtName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(txtText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtText;
    // End of variables declaration//GEN-END:variables
}
